package com.krt.facenettydemo.base;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;

import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.blankj.utilcode.util.CrashUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.krt.dao.DaoMaster;
import com.krt.dao.DaoSession;
import com.krt.facenettydemo.BuildConfig;
import com.krt.facenettydemo.UpdService;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author hyj
 * @time 2019/9/10 10:51
 * @class describe
 */
public class App extends Application {

    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();
//        initGreenDao();
        Utils.init(this);
        initLog();
        initCrash();
        activeEngine();
        startService(new Intent(this, UpdService.class));
    }

    private void initGreenDao() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "idcardveri.db");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public void initLog() {
        final LogUtils.Config config = LogUtils.getConfig()
                .setLogSwitch(BuildConfig.DEBUG)
                .setConsoleSwitch(BuildConfig.DEBUG)
                .setGlobalTag(null)
                .setLogHeadSwitch(true)
                .setLog2FileSwitch(false)
                .setDir("")
                .setFilePrefix("")
                .setBorderSwitch(true)
                .setSingleTagSwitch(true)
                .setConsoleFilter(LogUtils.V)
                .setFileFilter(LogUtils.V)
                .setStackOffset(0);
        LogUtils.d(config.toString());
    }

    @SuppressLint("MissingPermission")
    private void initCrash() {
        CrashUtils.init(Environment.getExternalStorageDirectory().getAbsolutePath()
                        + File.separator + "FaceNettyDemo" + File.separator + "crash",
                (crashInfo, e) -> LogUtils.e(crashInfo));
    }

    /**
     * 激活引擎
     */
    public void activeEngine() {
        FaceEngine faceEngine = new FaceEngine();
        Observable.create((ObservableOnSubscribe<Integer>) emitter -> {
            int activeCode = faceEngine.activeOnline(App.this, Constants.APP_ID, Constants.SDK_KEY);
            emitter.onNext(activeCode);
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer activeCode) {
                        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
                        int res = faceEngine.getActiveFileInfo(App.this, activeFileInfo);
                        if (res == ErrorInfo.MOK) {
                            Log.i("App", activeFileInfo.toString());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
